<?php

namespace App\Http\Controllers\CallingCards\Wechat;

use App\Http\Controllers\BaseController;
use App\Models\FeishuCallingCard\FeishuCallingCard;
use App\Models\FeishuCallingCard\FeishuCallingCardUser;
use App\Models\FeishuCallingCard\FeishuCallingCardUsersStoriesLog;
use App\Service\CallingCards\StoryImageService;
use App\Service\CallingCards\StoryService;
use App\Service\CallingCards\UsersStoriesLogService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class StoryController extends BaseController
{
    public function index(Request $request)
    {
        $limit = (int)$request->get('limit', 2);
        return $this->success(['list' => StoryService::index($limit)]);
    }

    public function detail(Request $request)
    {
        $id = (int)$request->id;
        $invitorId = (string)$request->invitorId;
        if ($invitorId > 1 || 'u' === mb_substr($invitorId, 0, 1)) {
            auth('callingCardsWeChat')->user()->invitorId = $invitorId;
            auth('callingCardsWeChat')->user()->save();
        }
        /*$url = null;
        foreach (StoryService::index() as $item) {
            if ($item['id'] == $id || null === $url) {
                $url = $item['url'];
            }
        }*/

        $user = auth('callingCardsWeChat')->user();
        $info = StoryService::index(1, $id)[0] ?? [];
        if (!$info) {
            return $this->fail(404, '文章不存在');
        }

        // 当手机号不存在时 前端提示去授权
        $info['isShowAuthButton'] = false;
        if (!strlen($user->mobile)) {
            $info['isShowAuthButton'] = UsersStoriesLogService::isShowAuthButton($user->id);// 是否显示授权按钮
        }
        $res                      = [
            'url'  => $info['url'] ?? null,
            'info' => $info,
        ];
        return $this->success($res);
    }

    public function shareImage(Request $request)
    {
        $this->validate($request, [
            'urlId' => 'required|int',
        ]);
        $storyTitle = null;
        foreach (StoryService::index() as $item) {
            if ($item['id'] == $request->urlId) {
                $storyTitle = $item['title'] ?? null;
            }
        }
        if (null === $storyTitle) {
            return $this->fail(404, '文章不存在');
        }
        $user = auth('callingCardsWeChat')->user();
        return $this->success(StoryImageService::make($user->invitorId, $request->urlId, $storyTitle));
    }

    /**
     * 文章阅读事件
     * @param Request $request
     * @return JsonResponse
     */
    public function article(Request $request): JsonResponse
    {
        $this->validate(
            $request, [
                'articlesId' => 'required|integer',
                'type'       => 'required|integer|' . Rule::in(array_keys(FeishuCallingCardUsersStoriesLog::TYPE_TEXT_MAPPING)),
            ]
        );
        $data['storiesId']       = (int)$request->get('articlesId', 0);      // 文章id
        $data['isReadCompleted'] = (int)$request->get('isReadCompleted', 0); // 是否完成阅读
        $data['isClickJoin']     = (int)$request->get('isClickJoin', 0);     // 是否点击加盟
        $data['type']            = (int)$request->get('type', 0);            // 类型 1-进入文章 2-心跳更新数据 3-主动关闭完成阅读

        $user = auth('callingCardsWeChat')->user();
        return $this->success();// 小程序文章阅读不记录了 调整为阅读微信服务号文章
//        return $this->success(UsersStoriesLogService::article($user, $data));
    }
}
